
//js特效开始
$(document).ready(function($) {
    // $(window).scroll(function() {
    //     if ($(this).scrollTop() > 100) {
    //         $('.back-to-top').fadeIn('slow');
    //     } else {
    //         $('.back-to-top').fadeOut('slow');
    //     }
    //     var $fixedheader = $('.header'); // fixed容器
    //     var wintop = $(window).scrollTop(); // 已滚动卷去的高度
    //     // 卷去一个可视窗口高度后执行
    //     if (wintop > 1) {
    //         $fixedheader.addClass("top-fixed");
    //     } else {
    //         $fixedheader.removeClass("top-fixed");
    //     }
    // });
    $('.back-to-top').click(function() {
        $('html, body').animate({
            scrollTop: 0
        }, 1500, 'easeInOutExpo');
        return false;
    });
 

    $('.nav-menu').superfish({
        animation: {
            opacity: 'show'
        },
        speed: 400
    });
 
    if ($('#nav-menu-container').length) {
        var $mobile_nav = $('#nav-menu-container').clone().prop({
            id: 'mobile-nav'
        });
        $mobile_nav.find('> ul').attr({
            'class': '',
            'id': ''
        });
        $('body').append($mobile_nav);
        $('body').prepend('<button type="button" id="mobile-nav-toggle"><i class="fa fa-bars"></i></button>');
        $('body').append('<div id="mobile-body-overly"></div>');
        $('#mobile-nav').find('.menu-has-children').prepend('<i class="fa fa-chevron-down"></i>');

        $(document).on('click', '.menu-has-children i', function(e) {
            $(this).next().toggleClass('menu-item-active');
            $(this).nextAll('ul').eq(0).slideToggle();
            $(this).toggleClass("fa-chevron-up fa-chevron-down");
        });

        $(document).on('click', '#mobile-nav-toggle', function(e) {
            $('body').toggleClass('mobile-nav-active');
            $('#mobile-nav-toggle i').toggleClass('fa-times fa-bars');
            $('#mobile-body-overly').toggle();
        });

        $(document).click(function(e) {
            var container = $("#mobile-nav, #mobile-nav-toggle");
            if (!container.is(e.target) && container.has(e.target).length === 0) {
                if ($('body').hasClass('mobile-nav-active')) {
                    $('body').removeClass('mobile-nav-active');
                    $('#mobile-nav-toggle i').toggleClass('fa-times fa-bars');
                    $('#mobile-body-overly').fadeOut();
                }
            }
        });
    } else if ($("#mobile-nav, #mobile-nav-toggle").length) {
        $("#mobile-nav, #mobile-nav-toggle").hide();
    }
 
    $('.nav-menu a, #mobile-nav a, .scrollto').on('click', function() {
        if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) {
            var target = $(this.hash);
            if (target.length) {
                var top_space = 0;

                if ($('#header').length) {
                    top_space = $('#header').outerHeight();

                    if (!$('#header').hasClass('header-fixed')) {
                        top_space = top_space - 20;
                    }
                }

                $('html, body').animate({
                    scrollTop: target.offset().top - top_space
                }, 1500, 'easeInOutExpo');

                if ($(this).parents('.nav-menu').length) {
                    $('.nav-menu .menu-active').removeClass('menu-active');
                    $(this).closest('li').addClass('menu-active');
                }

                if ($('body').hasClass('mobile-nav-active')) {
                    $('body').removeClass('mobile-nav-active');
                    $('#mobile-nav-toggle i').toggleClass('fa-times fa-bars');
                    $('#mobile-body-overly').fadeOut();
                }
                return false;
            }
        }
    });

    $('.portfolio-popup').magnificPopup({
        type: 'image',
        removalDelay: 300,
        mainClass: 'mfp-fade',
        gallery: {
            enabled: true
        },
        zoom: {
            enabled: true,
            duration: 300,
            easing: 'ease-in-out',
            opener: function(openerElement) {
                return openerElement.is('img') ? openerElement : openerElement.find('img');
            }
        }
    });

});


//通过文件路径截取名称
function subFileName(fileUrl) {
    if(fileUrl==null)return "";
    var index = fileUrl .lastIndexOf("\/");
    fileUrl  = fileUrl .substring(index + 1, fileUrl .length);
    return fileUrl
}

function ajaxToken1(url, type, data, success, fail) {
    $.ajax({
        url: url,
        type: type,
        data: data,
        beforeSend: function (request) {
            request.setRequestHeader("Authorization", window.localStorage.getItem("token"))
        },
        success: function (res) {
            if (res.code === 0 || res.code === '0') {
                success && success(res);
            } else if (res.code === 10000 || res.code === '10000') {
                // 登录态失效, 重新登录
                location.href = "/certificate/loginWeb"
            }else {
                success && success(res);
            }
        },
        error: function (res) {
            if (res.status == 401) {
                // 登录态失效, 重新登录
                location.href = "/certificate/loginWeb"
            } else {
                fail && fail(res);
                layer.confirm(res.errMsg);
            }
        }
    });
}

function ajaxToken(url, type, data, success, fail) {
    $.ajax({
        url: url,
        type: type,
        data: data,
        beforeSend: function (request) {
            request.setRequestHeader("Authorization", window.localStorage.getItem("token"))
        },
        success: function (res) {
            //console.log("222", res);
            if (res.code === 0 || res.code === '0') {
             success && success(res.data);
            } else if (res.code === 10000 || res.code === '10000') {
                // 登录态失效, 重新登录
                location.href = "/certificate/loginWeb"
            }  else if (res.code == 40013  || res.code === '40013') {
                 // success && success(res);
                layer.confirm("已经申请，去个人中心查看记录", {
                    btn: ['查看', '取消']
                }, function (index, layero) {
                    location.href = "/certificate/findApplyExam"
                })
            }else if (res.code == 40014  || res.code === '40014') {
                // success && success(res);
                layer.confirm("已经申请，去个人中心查看记录", {
                    btn: ['查看', '取消']
                }, function (index, layero) {
                    location.href = "/certificate/stationRecordList"
                })
            }else {
                layer.confirm(res.msg);
                return false;
            }
        },
        error: function (res) {
            if (res.status == 401) {
                // 登录态失效, 重新登录
                location.href = "/certificate/loginWeb"
            } else {
                fail && fail(res);
                layer.confirm(res.errMsg);
            }
        }
    });
}

function ajaxTokenForm(url, type, data, success, fail) {
    $.ajax({
        url: url,
        type: type,
        data: data,
        beforeSend: function (request) {
            request.setRequestHeader("Authorization", window.localStorage.getItem("token"))
        },
        success: function (res) {
            console.log("222", res);
            if (res.code === 0 || res.code === '0') {
                // layer.confirm('提交成功', {
                //     closeBtn: 0,
                //     btn: ['返回'],
                //     yes: function (index, layero) {
                //         location.href=document.referrer
                //     }
                // });
                success && success(res.data);
                //
            } else if (res.code === 10000 || res.code === '10000') {
                // 登录态失效, 重新登录
                location.href ="/certificate/loginWeb";
            } else if (res.code == 40013  || res.code === '40013') {
                 success && success(res);
            } else if(data.code==40026){
                    layer.confirm("订单生成错误，请重试")
                }else if (res.code == 10009) {
                layer.confirm("文件上传出错")
            } else {
                layer.confirm(res.msg);
                return false;
            }
        },
        error: function (res) {
            if (res.status == 401) {
                // 登录态失效, 重新登录
                location.href = "/certificate/loginWeb";
            } else {
                fail && fail(res);
                layer.alert(res.errMsg);
            }
        }
    });
}

